﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi.mMall.Seller
{
    public partial class mfAreaDjackVehicleList : msBsc.Cls.mBPProxyDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        private void getVehicleList()
        {
            string djackId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(djackId))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicle = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                int pIndex = 0;
                int.TryParse(this.GetParamHtValue("pIndex"), out pIndex);
                int pRows = 0;
                int.TryParse(this.GetParamHtValue("pRows"), out pRows);
                DataSet ds = vehicle.GetVehicleForPageByDjkId(pRows, pIndex, djackId);
                if (ds == null || ds.Tables[0].Rows.Count < 1)
                {
                    this.ExecJScript(@"$('#loginPage').hide();$('#nextPage').hide();");
                    return;
                }
                DataColumn dc = new DataColumn("VEHICLE_STATUS_NAME");
                ds.Tables[0].Columns.Add(dc);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ds.Tables[0].Rows[i]["VEHICLE_STATUS_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleStatusEnum), ds.Tables[0].Rows[i]["VEHICLE_STATUS"]);
                }

                DataColumn brandName = new DataColumn("VEHICLE_BRAND_NAME");
                ds.Tables[0].Columns.Add(brandName);
                DataColumn typeName = new DataColumn("VEHICLE_SERVICE_TYPE_NAME");
                ds.Tables[0].Columns.Add(typeName);
                foreach (DataRow item in ds.Tables[0].Rows)
                {
                    //车辆品牌
                    item["VEHICLE_BRAND_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.VehicleBrandEnum), Convert.ToInt32(item["VEHICLE_BRAND"].ToString()));

                    //服务类型
                    item["VEHICLE_SERVICE_TYPE_NAME"] = Enum.GetName(typeof(msBsc.AppLogic.DataAlc.cls.CommonEnum.ServiceTypeEnum), Convert.ToInt32(item["VEHICLE_SERVICE_TYPE"].ToString()));
                }

                string retJson = msBsc.AppLogic.Cfg.HttpTools.GetJsonByTable(ds.Tables[0], "", "", 1, 100, "");
                this.SetContrlValue("carNumber", ds.Tables[0].Rows[0]["DATACOUNT"].ToString());
                this.ExecJScript(@"
                            var _html = $('#svc_list_temp_vehicle').html();

                            laytpl(_html).render(" + retJson + @", function (render) {
                                $('#carList').append(render);});setPage(" + Convert.ToInt32(ds.Tables[0].Rows[0]["DATACOUNT"].ToString()) + "," + pIndex + ");");
            }
        }

        private void RefreshDatetime()
        {
            string vehicleID = this.GetParamHtValue("vehicleId");
            if (string.IsNullOrEmpty(vehicleID))
            {
                this.Alert("没有找到该车辆信息");
                return;
            }
            string djackId = msBsc.SvRef.UserInfo.Operator;
            if (string.IsNullOrEmpty(djackId))
            {
                return;
            }
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo vehicle = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                string msg = vehicle.RefreshDatetime(djackId, vehicleID);

                this.Alert(msg.Split(',')[1]);
            }
        }

        private void RemoveVehicle()
        {
            string vehicleId = this.GetParamHtValue("vehicleId");
            if (string.IsNullOrEmpty(vehicleId))
            {
                this.ExecJScript("alert('车辆信息有误,请检查')");
                return;
            }

            string result = string.Empty;
            using (msBsc.AppLogic.DataAlc.CBizVehicleInfo objCBizVehicleInfo = new AppLogic.DataAlc.CBizVehicleInfo())
            {
                result = objCBizVehicleInfo.RemoveVehicleInfo(vehicleId);
            }

            if (!string.IsNullOrEmpty(result))
            {
                this.ExecJScript("alert('" + result + "')");
                return;
            }
            this.ExecJScript(@"window.location.href='/ms_DjackVehicleList.htm'");
        }

        public override void DoAction()
        {
            if (this.strAction == "init")
            {
                getVehicleList();
            }
            else if (this.strAction == "RefreshDatetime")
            {
                RefreshDatetime();
            }
            else if (this.strAction == "removeVehicle")
            {
                RemoveVehicle();
            }
        }
    }
}